package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureMapper;
import com.itheima.pojo.Picture;
import com.itheima.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

@Service
public class PictureServiceImpl implements PictureService {
    @Autowired
    private PictureMapper pictureMapper;



    /**
     * 增加图片
     * @param picture
     */
    @Override
    public void add(Picture picture) {
        pictureMapper.insertSelective(picture);
    }

    /**
     * 删除图片
     * @param picture
     */
    @Override
    public void delete(Picture picture) {
        picture.setStatus(0);
        pictureMapper.updateByPrimaryKeySelective(picture);
    }

    /**
     * 修改图片
     * @param picture
     */
    @Override
    public void edit(Picture picture) {
        pictureMapper.updateByPrimaryKeySelective(picture);
    }

    /**
     * 分页查询
     * @param searchMap
     * @return
     */
    @Override
    public Page<Picture> findPage(Map<String, Object> searchMap) {
        Integer currentPage=null;
        if(searchMap.get("currentPage")!=null){
            currentPage = (Integer) searchMap.get("currentPage");
        }else {
            currentPage =1;
        }
        Integer pageSize=null;
        if( searchMap.get("pageSize")!=null){
            pageSize = (Integer) searchMap.get("pageSize");
        }else {
            pageSize=12;
        }
        Example example = new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 1);
        PageHelper.startPage(currentPage,pageSize);
        return (Page<Picture>) pictureMapper.selectByExample(example);
    }

    /**
     * 将图片设置为封面
     * 功能不清晰
     * @param picture
     */
    @Override
    public void setCover(Picture picture) {
    }
}
